/*
 	 	 	 	 	 	 	 	 *******************
 ******************************* C SOURCE FILE *******************************
 ** 							 *******************						   **
 ** 																		   **
 ** project : ClockSync 											           **
 ** filename :Timer															   **
 ** version : 1 															   **
 ** date : -	2012.04.10.													   **
 ** 																		   **
 *****************************************************************************
 ** 																		   **
 ** Copyright (c) 2012,		   							  				       **
 ** All rights reserved.													   **
 ** 																		   **
 *****************************************************************************
 VERSION HISTORY:
 ----------------
 Version : 1
 Date : -
 Revised by : -
 Description : Original version.
 */
/****************************************************************************/
/** 																	   **/
/** 							MODULES USED 							   **/
/** 																	   **/
/****************************************************************************/
#include <cyg/hal/hal_platform_ints.h>
#include <cyg/hal/lpc2106_registers.h>
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_diag.h>
#include <cyg/infra/diag.h>
#include <cyg/hal/hal_io.h>

#include <com_r04_s01b.h>
#include <dpy_trm_s01.h>
#include "platform.h"

#include <UserTasks.h>
#include <Timer.h>

/****************************************************************************/
/** 																	   **/
/** 							DEFINITIONS AND MACROS 					   **/
/** 																	   **/
/****************************************************************************/

/****************************************************************************/
/** 																	   **/
/** 							TYPEDEFS AND STRUCTURES 				   **/
/** 																	   **/
/****************************************************************************/

/****************************************************************************/
/** 																	   **/
/** 							GLOBAL VARIABLES 						   **/
/** 																	   **/
/****************************************************************************/

/****************************************************************************/
/** 																	   **/
/** 							PROTOTYPES OF FUNCTIONS 				   **/
/** 																	   **/
/****************************************************************************/


/****************************************************************************/
/** 																	   **/
/** 							FUNCTIONS 						           **/
/** 																	   **/
/****************************************************************************/

void cyg_user_start(void)
{
	/* Set NODE-ID */
	/* TODO it will be read from DIP-Switches */
	NODE_ID = 2;


	diag_printf(
			"Clock syncr program started at NODE:%d - firmware compiled: (%s -- %s)\r\n",
			NODE_ID, __DATE__, __TIME__);


	/* init MitMot API  */
	dpy_trm_s01__Init();

	/* init comm_layer */
	com_r04_s01__Init_433(SUB_BAND_434_2_MHz, BASEBAND_BW_270_kHz,
			FSK_DEVIATION_90_kHz, OUTPUT_POWER_MAX_minus_0dB,
			DATA_RATE_9600_bps);

	/* init flags and mutexes */
	cyg_flag_init(&Timed_clock_sync_event);
	cyg_mutex_init(&Com_layer_send_mutex);

	/* init tasks */

	/* create timed_clock_sync_request task */
	cyg_thread_create(TIMED_CLOCK_SYNC_REQUEST_PRIOR, // priority
			Timed_clock_sync_request, // thread function
			0, // thread parameter
			"timed_clock_sync_request_thread", // thread name
			&Timed_clock_sync_request_stack, // stack base addr
			TASK_STACK_SIZE, // stack size
			&Timed_clock_sync_request_handle, // thread handle
			&Timed_clock_sync_request_request_obj); // thread object

	/* create timed_clock_sync_request task*/
	cyg_thread_create(TIMED_CLOCK_SYNC_ANSWER_PRIOR, // priority
			Timed_clock_sync_answer, // thread function
			0, // thread parameter
			"timed_clock_sync_answer_thread", // thread name
			&Timed_clock_sync_answer_stack, // stack base addr
			TASK_STACK_SIZE, // stack size
			&Timed_clock_sync_answer_handle, // thread handle
			&Timed_clock_sync_answer_obj); // thread object

	/* create recieve_task */
	cyg_thread_create(COMMON_RECIEVE_PRIOR, // priority
			Common_recieve, // thread function
			0, // thread parameter
			"common_recieve thread", //	 thread name
			&Common_recieve_stack, // stack base addr
			TASK_STACK_SIZE, // stack size
			&Common_recieve_handle, // thread handle
			&Common_recieve_obj); // thread object

	// Creating an interrupt for Timer 1
	cyg_interrupt_create(CYGNUM_HAL_INTERRUPT_TIMER1, // interrupt vector
			ISR_PRIOR, // priority
			0, // Data
			Timer1_isr, // ISR function
			Timer1_dsr, // DSR function
			&timer1_int_handle, // interrupt handle
			&timer1_int); // interrupt object

	/* init timer */
	Timer1_init();

	/* init the snyc_clock to default */
	Init_sync_clcok();

	/* attach the created interrupt */
	cyg_interrupt_attach(timer1_int_handle);
	cyg_interrupt_unmask(CYGNUM_HAL_INTERRUPT_TIMER1);

	/* call the tasks */
	cyg_thread_resume(Common_recieve_handle);
	cyg_thread_resume(Timed_clock_sync_answer_handle);
	cyg_thread_resume(Timed_clock_sync_request_handle);
}

/****************************************************************************/
/** 																	   **/
/** 							EOF 							   		   **/
/** 																	   **/
/****************************************************************************/
